package com.fingard.dsp.bank.directbank.psbc01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IAcceptNotice;
import com.fingard.generalobj.NoticeReq;
import com.fingard.generalobj.NoticeResp;
import com.fingard.net.FtpReceiver;
import com.fingard.net.listener.SocketReceiver;
import com.fingard.xml.XmlTextReader;

/**
 * @author XueJy on 2018/12/18.
 * @version 1.0
 */
public class PSBC01AcceptNotice extends PSBC01Base implements IAcceptNotice {

    @Override
    public NoticeResp dealNotice(NoticeReq pNtReq) throws Exception {

    	NoticeResp resp = new NoticeResp();
    	String p_req = pNtReq.getReqString();
    	WriteBankLogLn("接收到请求：" + pNtReq.getFullString());
    	
        String tmpMsg = "";
        //WriteBankLogStep3(p_req);
        if (p_req.contains("<")) {
            tmpMsg = p_req.substring(p_req.indexOf('<'));
        }
        StringBuilder tmpSbMsg = new StringBuilder();
        //解析银行返回
        XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpMsg, tmpSbMsg);
        if (tmpBkReader != null) {
            String transtype = tmpBkReader.getFirstText("/root/Head/OpName");
            String filename = tmpBkReader.getFirstText("/root/body/FName");//文件名
            String transDate = tmpBkReader.getFirstText("/root/body/Trans_Date");//交易日期 YYYYMMDD
            //下载文件
            FtpReceiver ftpReceiver = fileDownload(filename, Format.Date8Format.parse(transDate));
            if (ftpReceiver.isCompleted) {
                String tmpStrRet = "ok";
                WriteBankLogLn("响应："+tmpStrRet);
                resp.respBytes = tmpStrRet.getBytes(pNtReq.reqCharset);
            	return resp;
            }
        }
        String tmpStrRet = "false";
        WriteBankLogLn("响应："+tmpStrRet);
        resp.respBytes = tmpStrRet.getBytes(pNtReq.reqCharset);
    	return resp;
    }

    @Override
    public void getContentLength(SocketReceiver p_receiver) throws Exception {

    }
}
